home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Can you printf a long
- Date: 4 Apr 1996 10:24 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <4APR199610244914@erich.triumf.ca>
- References: <4ju8o1$dc3@news.netam.net>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4ju8o1$dc3@news.netam.net>, bgc@alpha.netam.net (The Bowling Green Connection) writes...
- >I had some trouble with this code:
- >(I'm using gcc on Digital Unix)
- >
- >int main() {
- > long i;
- >
- > while(1) {
- > i++;
- > printf("%f\n", i);
- > }
- > return 0;
- >}
- >
- >The numbers printed to the screen as 0.00000.
-
- %f is used to print floats - what happens when you pass it an integer type (int
- or long) is undefined, but highly unlikely to produce any meaningful result.
-
- >But when I changed %f to %d, the numbers printed correctly.
- >I thought that %d had the same limitations as an int..that is,
- >it could only print about 4 bits of information, whereas a long
- >is capable of more (8 or 16 bits), so wouldn't %f (float) be able
- >to better handle those long numbers? And why did %d work
- >correctly, even up to 634,000 (that's when my disk space ran out.. :))
-
- You mean "bytes", not "bits".
-
- An int must be _at_ _least_ 2 bytes, but may be longer. A long must be at
- least as long as an int, but may (or may not) be longer. It seems that on your
- system, both int and long are 32 bits (4 bytes).
-
- (Under MS-DOS, an int is (for most compilers) 16 bits, and a long is 32 bits.)
-
- Floats are stored in a quite different format than ints and longs.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
- or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
-
-
-
-
-
-
-